package com.dianneng.show.util;

import java.util.ArrayList;
import java.util.List;

public class DateUtil {

    public static class DateInfo{
        private int year;
        private int month;
        private int day;

        public DateInfo() {
        }

        public DateInfo(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }

        public int getYear() {
            return year;
        }

        public void setYear(int year) {
            this.year = year;
        }

        public int getMonth() {
            return month;
        }

        public void setMonth(int month) {
            this.month = month;
        }

        public int getDay() {
            return day;
        }

        public void setDay(int day) {
            this.day = day;
        }
    }

    public static List<DateInfo> dateList=new ArrayList<>();

    public static int infoIndex=0;

    public static DateInfo getCurrentDate(){
        if (dateList.isEmpty()){
            initDateList();
        }
        infoIndex=0;
        return dateList.get(infoIndex);
    }

    public static DateInfo getNextDate(DateInfo dateInfo){
        if (dateList.isEmpty()){
            initDateList();
        }
        infoIndex=dateList.indexOf(dateInfo);
        infoIndex--;
        if (infoIndex<0){
            infoIndex=0;
        }
        return dateList.get(infoIndex);
    }

    public static DateInfo getPreDate(DateInfo dateInfo){
        if (dateList.isEmpty()){
            initDateList();
        }
        infoIndex=dateList.indexOf(dateInfo);
        infoIndex++;
        if (infoIndex>=dateList.size()){
            infoIndex=dateList.size()-1;
        }
        return dateList.get(infoIndex);
    }

    public static void initDateList(){
        int year=CommonUtil.getCurrentYear();
        int month=CommonUtil.getOneMonth();
        int day=CommonUtil.getCurrentDay();
        dateList.clear();

        DateInfo dateInfo=new DateInfo(year,month,day);
        dateList.add(dateInfo);
        for (int i=0;i<12;i++){
            month--;
            day=1;
            if (month<1){
                month=month+12;
                year--;
            }
            dateInfo=new DateInfo(year,month,day);
            dateList.add(dateInfo);
        }
    }

    public static void clearData(){
        if (dateList!=null && dateList.size()>0){
            dateList.clear();
        }
    }
}
